/**
 * Permissions
 * @typedef {object} Permissions
 * @property {string|array<string>} methods.required - 请求方法:*,POST,GET,PATCH,PUT,DELETE
 * @property {string} resource.required - 资源路径 - _path
 * @property {string} action.required - 操作（*|allow允许，deny拒绝，） - enum:*,allow,deny
 */

/**
 * Rules
 * @typedef {object} Rules
 * @property {array<number>} menus.required - 权限菜单（菜单ID）
 * @property {string} action.required - 权限菜单（菜单ID）- 操作（allow允许，deny拒绝，） - enum:allow,deny
 * @property {string} group.required - 角色（分组）
 */

/**
 * RulesEdit
 * @typedef {object} RulesEdit
 * @property {array<number>} menus - 权限菜单（菜单ID）
 * @property {string} action - 权限菜单（菜单ID）- 操作（allow允许，deny拒绝，） - enum:allow,deny
 * @property {string} group - 角色（分组）
 */

const jsonServer = require("json-server");
const { GroupExistError } = require("../CustomError.class");
const server = jsonServer.create();
const router = jsonServer.router("./db.json");

/* 更新数据缓存 */
server.use((req, res, next) => {
  router.db.read();
  next();
});

/**
 * GET /rules
 * @tags rules - 权限管理
 * @summary 获取权限列表
 * @security auth
 * @param {string} group.query - 角色（分组）
 * @param {number} _page.query - 页码
 * @param {number} _limit.query - 每页条数
 * @param {string} _sort.query - 排序字段 - enum:id,group,ctime,mtime
 * @param {string} _order.query - 排序规则 - enum:asc,desc
 * @return {Rules} 200 - success response
 */
server.get("/rules", (req, res, next) => {
  next();
});

/**
 * POST /rules
 * @tags rules
 * @summary 添加角色和权限
 * @security auth
 * @param {Rules} request.body.required - 权限
 * @return {Rules} 200 - success response
 */

server.post("/rules", (req, res, next) => {
  // 角色重复验证
  if (router.db.get("rules").find({ group: req.body.group }).value())
    throw new GroupExistError();
  next();
});

/**
 * PATCH /rules/{id}
 * @tags rules
 * @summary 编辑角色和权限
 * @security auth
 * @param {number} id.path.required - 编号
 * @param {RulesEdit} request.body.required
 * @return {Rules} 200 - success response
 */
server.patch("/rules/:id", (req, res, next) => {
  // 角色名称重复验证
  if (router.db.get("rules").find({ group: req.body.group }).value())
    throw new GroupExistError();
  next();
});

/**
 * GET /rules/{id}
 * @tags rules
 * @summary 根据ID获取角色权限
 * @security auth
 * @param {number} id.path.required - 编号
 * @return {Rules} 200 - success response
 */
server.get("/rule/:id", (req, res, next) => {
  next();
});

/**
 * DELETE /rules/{id}
 * @tags rules
 * @summary 根据ID删除角色权限
 * @security auth
 * @param {number} id.path.required - 编号
 * @return {Rules} 200 - success response
 */
server.delete("/rules/:id", (req, res, next) => {
  next();
});

/**
 * GET /rules/exist
 * @tags rules
 * @summary 判断字段是否存在
 * @security auth
 * @param {string} key.query.required - 字段名
 * @param {string} value.query.required - 值
 * @return {boolean} 200 - success response
 */
server.get("/rules/exist", (req, res, next) => {
  // 手机重复验证
  let data = false;
  if (
    router.db
      .get("rules")
      .find({ [req.query.key]: req.query.value })
      .value()
  ) {
    data = true;
  } else {
    data = false;
  }

  res.json({
    status: 200,
    success: true,
    data,
  });
});

module.exports = () => server;
